package com.ws.shop.action.admin;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.codec.digest.DigestUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ws.shop.bean.Page;
import com.ws.shop.entity.Member;
import com.ws.shop.service.MemberService;
import com.ws.shop.util.DateUtil;
import com.ws.shop.util.RequestUtil;

@Controller
@RequestMapping(value="/admin/member/*")
public class MemberAction extends BaseAdminAction{
	private static Logger log = Logger.getLogger(MemberAction.class);
	@Autowired
	private MemberService memberService;
	
	@RequestMapping(value="index.jhtml",method=RequestMethod.GET)
	public String index(HttpServletRequest request,Model model){
		int q_robot = RequestUtil.getIntParameter(request, "q_robot", 0);
		String q_mail = RequestUtil.getStringParameter(request, "q_mail", null);
		String q_name = RequestUtil.getStringParameter(request, "q_name", null);
		int pageno = RequestUtil.getIntParameter(request, Page.PAGE_NO, 1);
		try{
			Page page = memberService.findMemberListPage(pageno,0,q_name,q_mail,q_robot);
			model.addAttribute(Page.PAGE, page);
			model.addAttribute("q_robot", q_robot);
			model.addAttribute("q_mail", q_mail);
			model.addAttribute("q_name", q_name);
			model.addAttribute("stateDescs", Member.STATE_DESCS);
			return "member_index";
		}catch(Exception e){
			log.error("查询用户列表失败",e);
			model.addAttribute(ERROR_MSG, "系统异常，请稍后再试");
			return ERROR;
		}
	}

	@SuppressWarnings("unchecked")
	//选择头像
	public String selecthead(HttpServletRequest request,Model model){
		try{
			List headList = memberService.findHeadRandList(36);
			model.addAttribute("headList", headList);
			return "member_selecthead";
		}catch(Exception e){
			log.error("查询头像列表失败",e);
			model.addAttribute(ERROR_MSG,"系统异常，请稍后再试");
			return ERROR;
		}
	}
	
	@RequestMapping(value="input.jhtml",method=RequestMethod.GET)
	public String input(HttpServletRequest request,Model model){
		int id = RequestUtil.getIntParameter(request, "id", 0);
		if(id > 0){
			Member member = memberService.findMemberById(id);
			if(member != null){
				model.addAttribute("member", member);
			}else{
				model.addAttribute(ERROR_MSG, "用户不存在");
				return ERROR;
			}
		}
		model.addAttribute("id",id);
		return "member_input";
	}
	
	@RequestMapping(value="save.jhtml",method=RequestMethod.POST)
	public String save(HttpServletRequest request,HttpServletResponse response){
		try{
		int id = RequestUtil.getIntParameter(request, "id", 0);
		String mail = RequestUtil.getStringParameter(request, "mail", null);
		String name = RequestUtil.getStringParameter(request, "name", null);
		String password = RequestUtil.getStringParameter(request, "password", null);
		String ip = RequestUtil.getStringParameter(request, "ip", "");
		String head = RequestUtil.getStringParameter(request, "head", "");
		int sex = RequestUtil.getIntParameter(request, "sex", 1);
		String birthday = RequestUtil.getStringParameter(request, "birthday", null);
		String tel = RequestUtil.getStringParameter(request, "tel", "");
		int beat = RequestUtil.getIntParameter(request, "beat", 0);
		int pointb = RequestUtil.getIntParameter(request, "pointb", 0);
		int pointc = RequestUtil.getIntParameter(request, "pointc", 0);
		int robot = RequestUtil.getIntParameter(request, "robot", 0);
		int state = RequestUtil.getIntParameter(request, "state", 0);
		Member user = new Member(id,name,mail,password == null ? null : DigestUtils.md5Hex(password),name,ip,
				head,sex,birthday==null?new Date():DateUtil.stringToDate(birthday,DateUtil.YYYY_MM_DD_HH_MM_SS),
				tel,0,beat,pointb,pointc,1,robot,null,"",0,0,state);
		try{
			memberService.saveOrUpdate(user);
			return this.ajaxJsonSuccessMessage(response, "保存成功");
		}catch(Exception e){
			log.error("保存失败："+user,e);
			return this.ajaxJsonErrorMessage(response, "系统异常，保存失败");
		}
		}catch(Exception e){
			e.printStackTrace();
			return null;
		}
	}
	
	public String update(HttpServletRequest request,HttpServletResponse response){
		int id = RequestUtil.getIntParameter(request, "id", 0);
		String password = RequestUtil.getStringParameter(request, "password", null);
		String ip = RequestUtil.getStringParameter(request, "ip", null);
		String head = RequestUtil.getStringParameter(request, "head", null);
		int sex = RequestUtil.getIntParameter(request, "sex", 1);
		String birthday = RequestUtil.getStringParameter(request, "birthday", null);
		String tel = RequestUtil.getStringParameter(request, "tel", null);
		int beat = RequestUtil.getIntParameter(request, "beat", 0);
		int pointb = RequestUtil.getIntParameter(request, "pointb", 0);
		int pointc = RequestUtil.getIntParameter(request, "pointc", 0);
		int robot = RequestUtil.getIntParameter(request, "robot", 0);
		int state = RequestUtil.getIntParameter(request, "state", 0);
		Member user = new Member(id,null,null,DigestUtils.md5Hex(password),"",ip,
				head,sex,birthday==null?new Date():DateUtil.stringToDate(birthday,DateUtil.YYYY_MM_DD),
				tel,0,beat,pointb,pointc,1,robot,null,"",0,0,state);
		try{
			memberService.saveOrUpdate(user);
			return this.ajaxJsonSuccessMessage(response, "编辑成功");
		}catch(Exception e){
			log.error("编辑失败："+user,e);
			return this.ajaxJsonErrorMessage(response, "系统异常，保存失败");
		}
	}
	
}
